(function() {
  var update_batch_button_status;

  update_batch_button_status = function() {
    var batch_checkable, batch_deletable, batch_deletable_ex, batch_disable, batch_editable, batch_enable, box, checked_boxes, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _len5, _m, _n;
    if (page_perms.check) {
      $('button#batch_check').removeClass('has_clicked').text('批量复核');
    }
    if (page_perms.disable) {
      $('button#batch_disable').removeClass('has_clicked').text('批量禁用');
      $('button#batch_enable').removeClass('has_clicked').text('批量启用');
    }
    if (page_perms["delete"]) {
      $('button#batch_delete').removeClass('has_clicked').text('批量删除');
    }
    checked_boxes = $('tbody .checkbox.checked#batch');
    if (checked_boxes.length === 0) {
      $('button#batch_check').addClass('disabled');
      $('button#batch_disable').addClass('disabled');
      $('button#batch_enable').addClass('disabled');
      $('button#batch_delete').addClass('disabled');
      return;
    }
    batch_editable = true;
    batch_checkable = true;
    batch_disable = true;
    batch_enable = true;
    batch_deletable = true;
    batch_deletable_ex = true;
    if (page_perms.batch && page_perms.edit) {
      for (_i = 0, _len = checked_boxes.length; _i < _len; _i++) {
        box = checked_boxes[_i];
        if (!$(box).data('batch-editable')) {
          batch_editable = false;
          break;
        }
      }
    } else {
      batch_editable = false;
    }
    if (page_perms.batch && page_perms.check) {
      for (_j = 0, _len1 = checked_boxes.length; _j < _len1; _j++) {
        box = checked_boxes[_j];
        if (!$(box).data('batch-checkable')) {
          batch_checkable = false;
          break;
        }
      }
    } else {
      batch_checkable = false;
    }
    if (page_perms.batch && page_perms.disable) {
      for (_k = 0, _len2 = checked_boxes.length; _k < _len2; _k++) {
        box = checked_boxes[_k];
        if (!$(box).data('batch-disable')) {
          batch_disable = false;
          break;
        }
      }
    } else {
      batch_disable = false;
    }
    if (page_perms.batch && page_perms.disable) {
      for (_l = 0, _len3 = checked_boxes.length; _l < _len3; _l++) {
        box = checked_boxes[_l];
        if (!$(box).data('batch-enable')) {
          batch_enable = false;
          break;
        }
      }
    } else {
      batch_enable = false;
    }
    if (page_perms.batch && page_perms["delete"]) {
      for (_m = 0, _len4 = checked_boxes.length; _m < _len4; _m++) {
        box = checked_boxes[_m];
        if (!$(box).data('batch-deletable')) {
          batch_deletable = false;
          break;
        }
      }
    } else {
      batch_deletable = false;
    }
    if (page_perms.batch && page_perms["delete"]) {
      for (_n = 0, _len5 = checked_boxes.length; _n < _len5; _n++) {
        box = checked_boxes[_n];
        if (!$(box).data('batch-deletable-ex')) {
          batch_deletable_ex = false;
          break;
        }
      }
    } else {
      batch_deletable_ex = false;
    }
    if (batch_checkable) {
      $('button#batch_check').removeClass('disabled');
    } else {
      $('button#batch_check').addClass('disabled');
    }
    if (batch_disable) {
      $('button#batch_disable').removeClass('disabled');
    } else {
      $('button#batch_disable').addClass('disabled');
    }
    if (batch_enable) {
      $('button#batch_enable').removeClass('disabled');
    } else {
      $('button#batch_enable').addClass('disabled');
    }
    if (batch_deletable !== batch_deletable_ex) {
      return $('button#batch_delete').removeClass('disabled');
    } else {
      return $('button#batch_delete').addClass('disabled');
    }
  };

  $(function() {
    var reset_batch_buttons;
    $('button#new').click(function() {
      return window.location = '/merch/new';
    });
    $('button#import').click(function() {
      return window.location = '/merch/import';
    });
    $('button#export').click(function() {
      return window.location = '/merch/export';
    });
    $('i#settl').click(function() {
      var uuid;
      uuid = $(this).data('uuid');
      return window.location = "/merch/settl?settl_merid=" + uuid;
    });
    $('i#input').click(function() {
      var uuid;
      uuid = $(this).data('uuid');
      return window.location = "/merch/input/" + uuid;
    });
    $('i#check').click(function() {
      var action, uuid;
      uuid = $(this).data('uuid');
      action = $(this).data('action');
      return window.location = "/merch/check/" + action + "/" + uuid;
    });
    $('button#batch').click(function() {
      $('th#status, td#status').hide();
      $('th#batch, td#batch').show();
      $('form#search_form').find('.input').addClass('disabled');
      $('tfoot').find('*').addClass('disabled').attr('disabled', 'disabled');
      $('.buttons#primary').hide();
      return $('.buttons#secondary').show('fast');
    });
    $('button#batch_exit').click(function() {
      $('th#batch, td#batch').hide();
      $('th#status, td#status').show();
      $('form#search_form').find('.input').removeClass('disabled');
      $('tfoot').find('*').removeClass('disabled').removeAttr('disabled');
      $('.buttons#secondary').hide();
      return $('.buttons#primary').show('fast');
    });
    $('.checkbox#batch').each(function() {
      return $(this).checkbox({
        onChecked: function() {
          return update_batch_button_status();
        },
        onUnchecked: function() {
          return update_batch_button_status();
        }
      });
    });
    reset_batch_buttons = function() {
      $('button#batch_check').removeClass('has_clicked').text('批量复核');
      $('button#batch_disable').removeClass('has_clicked').text('批量禁用');
      $('button#batch_enable').removeClass('has_clicked').text('批量启用');
      return $('button#batch_delete').removeClass('has_clicked').text('批量删除');
    };
    $('button#batch_check').click(function() {
      var records;
      if ($(this).hasClass('has_clicked')) {
        $(this).removeClass('has_clicked').text('批量复核');
        records = [];
        $('tbody .checkbox.checked#batch').each(function() {
          return records.push({
            serial: $(this).data('serial'),
            uuid: $(this).data('uuid')
          });
        });
        $(this).addClass('loading');
        return $.post('/merch/batch/check', {
          records: JSON.stringify(records)
        }, function(resp) {
          return $('button#batch_check').removeClass('loading');
        });
      } else {
        reset_batch_buttons();
        return $(this).addClass('has_clicked').text('确认批量复核');
      }
    });
    $('button#batch_disable').click(function() {
      var records;
      if ($(this).hasClass('has_clicked')) {
        $(this).removeClass('has_clicked').text('批量禁用');
        records = [];
        $('tbody .checkbox.checked#batch').each(function() {
          return records.push({
            serial: $(this).data('serial'),
            uuid: $(this).data('uuid')
          });
        });
        $(this).addClass('loading');
        return $.post('/merch/batch/disable', {
          records: JSON.stringify(records)
        }, function(resp) {
          return $('button#batch_disable').removeClass('loading');
        });
      } else {
        reset_batch_buttons();
        return $(this).addClass('has_clicked').text('确认批量禁用');
      }
    });
    $('button#batch_enable').click(function() {
      var records;
      if ($(this).hasClass('has_clicked')) {
        $(this).removeClass('has_clicked').text('批量启用');
        records = [];
        $('tbody .checkbox.checked#batch').each(function() {
          return records.push({
            serial: $(this).data('serial'),
            uuid: $(this).data('uuid')
          });
        });
        $(this).addClass('loading');
        return $.post('/merch/batch/enable', {
          records: JSON.stringify(records)
        }, function(resp) {
          return $('button#batch_enable').removeClass('loading');
        });
      } else {
        reset_batch_buttons();
        return $(this).addClass('has_clicked').text('确认批量启用');
      }
    });
    return $('button#batch_delete').click(function() {
      var deleteable_ex, records, url;
      if ($(this).hasClass('has_clicked')) {
        $(this).removeClass('has_clicked').text('批量删除');
        records = [];
        deleteable_ex = false;
        $('tbody .checkbox.checked#batch').each(function() {
          deleteable_ex = $(this).data('batch-deletable-ex');
          return records.push({
            serial: $(this).data('serial'),
            uuid: $(this).data('uuid')
          });
        });
        $(this).addClass('loading');
        url = '/merch/batch/delete';
        if (deleteable_ex) {
          url += "_ex";
        }
        return $.post(url, {
          records: JSON.stringify(records)
        }, function(resp) {
          return $('button#batch_delete').removeClass('loading');
        });
      } else {
        reset_batch_buttons();
        return $(this).addClass('has_clicked').text('确认批量删除');
      }
    });
  });

}).call(this);
